// 这个 http 是我们封装好的 axios
import http from "./http";
// 导入请求地址
import {
    nowPlayingListUri,
    comingSoonListUri,
    moiveDetailUrl,
    cityListUrl,
    cinemaListUrl,
    loginUrl,
    centerUrl,
    newsUrl
} from "@/config/url";
// 请求正在热映列表数据
export const nowPlayingListData = (pageNum: string) => {
    return http.get(nowPlayingListUri + pageNum, {
        headers: {
            info: "",
            authorization: ""
        }
    });
};
// 请求即将上映列表数据
export const comingSoonListData = (pageNum: string) => {
    return http.get(comingSoonListUri + pageNum);
};
// 请求电影详情数据
export const moiveDetailData = (filmId: string) => {
    // 给axios设置请求头
    return http.get(moiveDetailUrl + filmId, {
        headers: {
            info: "info",
            authorization: ""
        }
    });
};

// 城市列表数据
export const cityListData = async () => {
    // 给axios设置请求头
    const ret = await http.get(cityListUrl, {
        headers: {
            info: "city",
            authorization: ""
        }
    });
    // 定义基本的数据
    const cities = ret.data.cities;
    const codeList: string[] = []; //'A','B','C'....（完整26个字母）
    const dataList: object[] = []; // 城市信息
    const indexList: string[] = []; //'A','B','C'....（经过筛选的字母）

    // for循环生成26个字母
    for (let i = 65; i <= 90; i++) {
        codeList.push(String.fromCharCode(i));
    }
    // 开始处理
    codeList.forEach((element) => {
        // 与城市信息的pinyin字段首字母进行对比，如果符合则留下大字母
        const tempArr = cities.filter(
            (item: {
                pinyin: string
            }) => element.toLowerCase() == item.pinyin.slice(0, 1)
        );
        if (tempArr.length > 0) {
            indexList.push(element);
            dataList.push({
                index: element,
                data: tempArr,
            });
        }
    });
    console.log(dataList);
    // 返回数据
    return Promise.resolve([dataList, indexList]);
};

// 请求所在城市的电影院列表
export const cinemasList = (cityId: string) => {
    // 给axios设置请求头
    return http.get(cinemaListUrl + cityId, {
        headers: {
            info: "cinema",
            authorization: ""
        }
    });
};

// 请求咨询信息
export const cinemasNews = () => {
    // 给axios设置请求头
    return http.get(newsUrl, {
        headers: {
            info: "news",
            authorization: ""
        }
    });
};

// 登录
export const userLogin = (data: any) => {
    // data 的数据格式我们也要关注  x-www-form-urlencoded 格式数据
    let newStr = '';
    for (const item in data) {
        newStr += encodeURIComponent(item) + '=' + encodeURIComponent(data[item]) + '&';
    }
    newStr = newStr.slice(0, -1);
    return http.post(loginUrl, newStr, {
        headers: {
            info: "user",
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
};
// 获取用户个人信息
export const userInfo = (_token: string) => {
    return http.get(centerUrl, {
        headers: {
            info: "user",
            authorization: _token
        }
    });
};